<!-- 退场申请-用户 -->
<template>
	<view>
		<bg-tab :current="current" :tabs="tabs" @tab-change="tabChange"></bg-tab>
		<view class="m-3">
			<scroll-view @scrolltolower="loadMore" class="scroll-block" :scroll-y="true"
				refresher-background="transparent" refresher-enabled="true" :refresher-triggered="triggered"
				@refresherrefresh="onRefresh">
				<view v-if="dataList.length>0">
					<view class="bt-block py24px30 mb-3" @tap="gotoDetail(item)" v-for="item,idx in dataList"
						:key="idx">
						<view class="flex">
							<view class="flex-1">
								<bg-desc label="申请人" :value="item.createUserName"></bg-desc>
								<bg-desc label="项目名称" :valueStyle="{width:'150px'}" :value="item.projectName"></bg-desc>
								<bg-desc label="提交时间" :valueStyle="{width:'150px'}" :value="item.createTime"></bg-desc>
							</view>
							<view class="flex align-center">
								<u--image v-if="params.status==1" width="148rpx" height="148rpx"
									src="/static/images/ui/salary/wait.png"></u--image>
								<u--image v-if="params.status==2" width="148rpx" height="148rpx"
									src="/static/images/ui/salary/pass.png"></u--image>
								<u--image v-if="params.status==3" width="148rpx" height="148rpx"
									src="/static/images/ui/salary/unpass.png"></u--image>
							</view>
						</view>
					</view>
				</view>
				<u-empty v-if="dataList.length==0&&!triggered"></u-empty>
			</scroll-view>
		</view>
		<view class="bt-footer" v-if="isShowAuditBtn&&current==0">
			<bg-btn @click="allAudit">
				批量审批
			</bg-btn>
		</view>
		<bg-confirm :popupTitle="'批量审批'" :content="`确定对${auditTotal}条数据批量进行审批？`" @confirm="confirmModal"
			@close="closeModal" :show.sync="show">
		</bg-confirm>
	</view>
</template>

<script>
	import {
		retireLeaderList,
		retireList,
		exitDetail,
		leaderAuditbatch,
		managerAuditbatch
	} from "@/api/retire.js"
	export default {
		onNavigationBarButtonTap() {
			uni.navigateTo({
				url: "/pages/salary/retire/retire"
			})
		},
		onLoad(options) {
			this.getData()
			this.getUserInfo()
		},
		mounted() {
			uni.$on("refresh-retire", () => {
				this.params.pageNum = 1
				this.dataList = []
				this.getData()
			})
		},
		data() {
			return {
				triggered: false,
				_freshing: false,
				current: 0,
				tabs: [
					"待审批", "审批通过", "审批未通过"
				],
				params: {
					status: 1,
					projectId: this.$store.getters.projectId,
					pageNum: 1,
					pageSize: 20
				},
				dataList: [],
				total: 0,
				isShowAuditBtn: false,
				allAuditList: [],
				auditTotal: 0,
				show: false,
			}
		},
		methods: {
			getUserInfo() {
				if (this.$auth.hasRoleOr(["team", "project"])) {
					this.isShowAuditBtn = true
					retireLeaderList({
						status: 1,
						pageNum: 1,
						pageSize: 10000
					}).then(res => {
						let data = res.data.rows
						this.auditTotal = res.data.total
						let arr = []
						data.map((item) => {
							arr.push({
								id: item.id,
								status: 2
							})
							return item
						})
						this.allAuditList = arr
					})
				}

			},
			async onRefresh() {
				if (this._freshing) return;
				this._freshing = true;
				if (!this.triggered) {
					this.triggered = true;
				}
				this.dataList = []
				this.params.pageNum = 1
				await this.getData()
				setTimeout(() => {
					this.triggered = false;
					this._freshing = false;
				}, 2000)
			},
			loadMore() {
				if (this.dataList.length < this.total) {
					this.params.pageNum += 1
					this.getData()
				}
			},
			getData() {
				let method = retireList
				if (this.$auth.hasRoleOr(["team", "project"])) {
					method = retireLeaderList
				}

				method(this.params).then(res => {
					this.dataList = [...this.dataList, ...res.data.rows]
					this.total = res.data.total
				})
			},
			tabChange(idx) {
				this.current = idx
				this.params.status = idx + 1
				this.params.pageNum = 1
				this.dataList = []
				this.getData()
			},
			gotoDetail(i) {

				// if(i.signUrl){
				// 	let target = "/pages/common/webview/index?url="+encodeURIComponent(i.signUrl)+"&title=退场详情"
				// 	uni.navigateTo({
				// 		url:target
				// 	})
				// } else {
				// 	this.$modal.msg("无签约地址")
				// }
				uni.navigateTo({
					url: "/pages/salary/retire/detail?id=" + i.id
				})
				// uni.navigateTo({
				// 	url:"/pages/salary/retire/detail-native/detail-native?id="+i.id
				// })
			},
			confirmModal() {
				if(this.allAuditList.length<=0) return this.$modal.msg("暂无可审批数据")
				let method = ''
				if (this.$auth.hasRole("team")) {
					method = leaderAuditbatch
				} else if (this.$auth.hasRole("project")) {
					method = managerAuditbatch
				}
				method(this.allAuditList).then(res => {
					this.show = false;
				})
			},
			closeModal() {
				this.show = false;
			},
			allAudit() {
				this.show = true;
			}
		}
	}
</script>

<style>

</style>